package cn.growthgate.fgo.web;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.growthgate.fgo.common.Constants;
import cn.growthgate.fgo.entity.Item;
import cn.growthgate.fgo.entity.MyItem;
import cn.growthgate.fgo.entity.enums.ItemRare;
import cn.growthgate.fgo.entity.enums.ItemType;
import cn.growthgate.fgo.entity.enums.Role;
import cn.growthgate.fgo.entity.vo.MyItemVo;
import cn.growthgate.fgo.service.IItemService;
import cn.growthgate.fgo.service.IMyItemService;
import cn.growthgate.fgo.service.IMyServantService;
import cn.growthgate.fgo.web.result.JsonResult;

/**
 * <p>
 * 我的素材表 前端控制器
 * </p>
 *
 * @author GrowthGate
 * @since 2019-06-26
 */
@Controller
@RequestMapping("/myItem")
@RequiresRoles(value = {Constants.ROLE_ADMIN, Constants.ROLE_USER}, logical = Logical.OR)
public class MyItemController {

	@Autowired
	private IMyItemService myItemService;

	@Autowired
	private IItemService itemService;

	@Autowired
	private IMyServantService myServantService;

	@RequestMapping("/list")
	public ModelAndView list() {
		// 素材列表
		List<Item> bronzes = itemService
				.selectList(itemService.newWrapper().eq("type", ItemType.MATERIAL).eq("rare", ItemRare.BRONZE));
		List<Item> silvers = itemService
				.selectList(itemService.newWrapper().eq("type", ItemType.MATERIAL).eq("rare", ItemRare.SILVER));
		List<Item> golds = itemService
				.selectList(itemService.newWrapper().eq("type", ItemType.MATERIAL).eq("rare", ItemRare.GOLD));
		List<Item> pieces = itemService.selectList(itemService.newWrapper().eq("type", ItemType.PIECE));
		List<Item> biscuits = itemService.selectList(itemService.newWrapper().eq("type", ItemType.BISCUITS));
		// 获取用户数据
		MyItemVo usedQp = myServantService.getUsedQp();
		Map<Long, MyItemVo> usedItem = myServantService.getUsedItem();
		Map<Long, Integer> hold = myItemService.getAll();
		MyItemVo totalNeedQp = myServantService.getTotalNeedQp();
		Map<Long, MyItemVo> totalNeedItem = myServantService.getTotalNeedItem();

		return new ModelAndView("myItem/list").addObject("bronzes", bronzes).addObject("silvers", silvers)
				.addObject("golds", golds).addObject("pieces", pieces).addObject("biscuits", biscuits)
				.addObject("usedQp", usedQp).addObject("usedItem", usedItem).addObject("hold", hold)
				.addObject("totalNeedQp", totalNeedQp).addObject("totalNeedItem", totalNeedItem);
	}

	@ResponseBody
	@RequestMapping("/save")
	public JsonResult save(@Validated MyItem myItem, BindingResult bindingResult, Role[] roles) {
		if (bindingResult.hasErrors()) {
			return JsonResult.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
		}
		myItemService.save(myItem);
		return JsonResult.ok("保存成功");
	}

}
